package atomgit_hook

import (
	"encoding/json"
	"fmt"
	"testing"
)

var issueData = `
{
	"action": "opened",
	"issue": {
	  "url": "https://api.atomgit.com/repos/openlinksaas/desktop/issues/3",
	  "repository_url": "https://api.atomgit.com/repos/openlinksaas/desktop",
	  "labels_url": "https://api.atomgit.com/repos/openlinksaas/desktop/issues/3/labels{/name}",
	  "comments_url": "https://api.atomgit.com/repos/openlinksaas/desktop/issues/3/comments",
	  "events_url": "https://api.atomgit.com/repos/openlinksaas/desktop/issues/3/events",
	  "html_url": "https://atomgit.com/openlinksaas/desktop/issues/3",
	  "id": "64fd4869eff20a41b0799337",
	  "number": "3",
	  "title": "test3",
	  "user": {
		"id": "64fae54f49d9355e7a863eb6",
		"type": "User",
		"site_admin": false,
		"login": "openlinksaas",
		"avatar_url": "https://file.atomgit.com/uploads/user/1694222084215_2176.png",
		"url": "https://api.atomgit.com/openlinksaas",
		"html_url": "https://atomgit.com/openlinksaas",
		"followers_url": "https://api.atomgit.com/openlinksaas/followers",
		"following_url": "https://api.atomgit.com/openlinksaas/following{/other_user}",
		"organizations_url": "https://api.atomgit.com/openlinksaas/orgs",
		"repos_url": "https://api.atomgit.com/openlinksaas/repos"
	  },
	  "labels": [],
	  "state": "open",
	  "locked": false,
	  "assignee": null,
	  "milestone": {},
	  "created_at": "2023-09-10T12:39:05+08:00",
	  "updated_at": "2023-09-10T12:39:05+08:00",
	  "closed_at": null,
	  "body": "<p>1111</p>\n",
	  "state_reason": null
	},
	"repository": {
	  "id": 96897,
	  "name": "desktop",
	  "full_name": "openlinksaas/desktop",
	  "private": false,
	  "owner": {
		"id": "64fae54f49d9355e7a863eb6",
		"type": "User",
		"site_admin": false,
		"login": "openlinksaas",
		"avatar_url": "https://file.atomgit.com/uploads/user/1694222084215_2176.png",
		"url": "https://api.atomgit.com/openlinksaas",
		"html_url": "https://atomgit.com/openlinksaas",
		"followers_url": "https://api.atomgit.com/openlinksaas/followers",
		"following_url": "https://api.atomgit.com/openlinksaas/following{/other_user}",
		"organizations_url": "https://api.atomgit.com/openlinksaas/orgs",
		"repos_url": "https://api.atomgit.com/openlinksaas/repos"
	  },
	  "html_url": "https://atomgit.com/openlinksaas/desktop",
	  "description": "",
	  "url": "https://api.atomgit.com/repos/openlinksaas/desktop",
	  "hooks_url": "https://api.atomgit.com/repos/openlinksaas/desktop/hooks",
	  "branches_url": "https://api.atomgit.com/repos/openlinksaas/desktop/branches{/branch}",
	  "tags_url": "https://api.atomgit.com/repos/openlinksaas/desktop/tags",
	  "git_tags_url": "https://api.atomgit.com/repos/openlinksaas/desktop/git/tags{/sha}",
	  "git_refs_url": "https://api.atomgit.com/repos/openlinksaas/desktop/git/refs{/sha}",
	  "trees_url": "https://api.atomgit.com/repos/openlinksaas/desktop/trees{/sha}",
	  "commits_url": "https://api.atomgit.com/repos/openlinksaas/desktop/commits{/sha}",
	  "comments_url": "https://api.atomgit.com/repos/openlinksaas/desktop/comments{/number}",
	  "issue_comment_url": "https://api.atomgit.com/repos/openlinksaas/desktop/issues/comments{/number}",
	  "merges_url": "https://api.atomgit.com/repos/openlinksaas/desktop/merges",
	  "issues_url": "https://api.atomgit.com/repos/openlinksaas/desktop/issues{/number}",
	  "pulls_url": "https://api.atomgit.com/repos/openlinksaas/desktop/pulls{/number}",
	  "milestones_url": "https://api.atomgit.com/repos/openlinksaas/desktop/milestones{/number}",
	  "labels_url": "https://api.atomgit.com/repos/openlinksaas/desktop/labels{/name}",
	  "releases_url": "https://api.atomgit.com/repos/openlinksaas/desktop/releases{/id}",
	  "created_at": "2023-09-08 17:15:35",
	  "git_url": "git://atomgit.com/openlinksaas/desktop.git",
	  "ssh_url": "git@atomgit.com:openlinksaas/desktop.git"
	},
	"sender": {
	  "id": "64fae54f49d9355e7a863eb6",
	  "type": "User",
	  "site_admin": false,
	  "login": "openlinksaas",
	  "avatar_url": "https://file.atomgit.com/uploads/user/1694222084215_2176.png",
	  "url": "https://api.atomgit.com/openlinksaas",
	  "html_url": "https://atomgit.com/openlinksaas",
	  "followers_url": "https://api.atomgit.com/openlinksaas/followers",
	  "following_url": "https://api.atomgit.com/openlinksaas/following{/other_user}",
	  "organizations_url": "https://api.atomgit.com/openlinksaas/orgs",
	  "repos_url": "https://api.atomgit.com/openlinksaas/repos"
	}
}
`

func TestIssueHook(t *testing.T) {
	issueHook := &IssueHook{}
	err := json.Unmarshal([]byte(issueData), issueHook)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}

	data, _ := json.MarshalIndent(issueHook.ToProtoBuf(), "  ", "  ")
	fmt.Println(string(data))
}
